***********************************************************************************************************************
**** Table A17. Coarsened Exact Matching ******************************************************************************
***********************************************************************************************************************


use "$data/Non_payment_dataset_ready.dta" , clear

log using "$filepath/output_logs/table A17", replace



set seed 3125


* Install user-written cem command
* ssc install cem
preserve
gen treatmenttomatch=0 if itt==.
replace treatmenttomatch=1 if itt==0
drop if treatmenttomatch==. 
drop if t>12
collapse (mean) meanasinhinvoice inactive prenoconsumption paidcount1 paidcount2 meanaveragepayment1 meanaveragepayment2 preclosingbalancew  presumpaymentratiow  preage_of_accountw if t<209, by(customer treatmenttomatch)
cem  meanasinhinvoice (#5) inactive (#5) prenoconsumption(#5) paidcount1 (#5) paidcount2 (#5) meanaveragepayment1(#5) meanaveragepayment2(#5) preclosingbalancew(#5)  presumpaymentratiow(#5) preage_of_accountw(#5), treatment(treatmenttomatch) k2k 
bysort treatmenttomatch: tabstat meanasinhinvoice inactive prenoconsumption paidcount1 paidcount2 meanaveragepayment1 meanaveragepayment2 preclosingbalancew  presumpaymentratiow preage_of_accountw [aweight=cem_weights] if cem_matched==1, by(treatmenttomatch) s(me v) nototal

keep if cem_matched==1
gen matchedcontrol=1

keep customer matchedcontrol cem_weights
save "$filepath/temp/matchedcontrol", replace // generates a new_datafile with the matching weights
restore

** merge in matched control group *
merge m:1 customer using "$filepath/temp/matchedcontrol", keepusing(matchedcontrol cem_weights) nogen update
bysort customer: egen matchedcontrol2=mean(matchedcontrol)
replace matchedcontrol=matchedcontrol2
drop matchedcontrol2

gen cem_match=0 if matchedcontrol==1 & itt==.
replace cem_match=1 if itt==0 & matchedcontrol==1


*********************************
**** Initial month (October) ****
*********************************

probit paidcount cem_match  if t==14 ,vce(cluster customer)
margins, dydx(*) post

reg lnpayment cem_match if t==14, vce(cluster customer)

preserve 
keep if t==14
capture program drop Ey_boot
program define Ey_boot, eclass
twopm payment cem_match, firstpart(probit) secondpart(regress, log) vce(cluster customer)
margins, dydx(cem_match) predict(duan) nose post
end
bootstrap _b, seed(3125) reps(1000): Ey_boot
restore

reg asinhpayment cem_match if t==14, vce(cluster customer)


*************************************
**** Medium term (November-June) ****
*************************************

probit paidcount cem_match if t>=15 & t<=22 ,vce(cluster customer)
margins, dydx(*) post

reg lnpayment cem_match if t>=15 & t<=22 , vce(cluster customer)


preserve 
keep if t>=15 & t<=22 
capture program drop Ey_boot
program define Ey_boot, eclass
twopm payment cem_match i.t, firstpart(probit) secondpart(regress, log) vce(cluster customer)
margins, dydx(cem_match) predict(duan) nose post
end
bootstrap _b, seed(3125) reps(1000): Ey_boot
restore

reg asinhpayment cem_match  if t>=15 & t<=22, vce(cluster customer)


estpost tabstat paidcount lnpayment payment asinhpayment if t==14 & cem_match==0, by(cem_match) statistics(mean sd n) columns(statistics) nototal

estpost tabstat  paidcount lnpayment payment asinhpayment if t>=15 & t<=22 & cem_match==0, by(cem_match) statistics(mean sd n) columns(statistics) nototal

log close
